﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Lite.ExcelLibrary.SpreadSheet;
using System.IO;
using SilverlightOracle.WCFService;

namespace SilverlightOracle
{
    public partial class InsertThiSinh : ChildWindow
    {
        ServiceClient webservice = new ServiceClient();
        int manguoidung = 0;
        int[] check  = new int[1000];
        int n = 0;
        Worksheet sh;
        long maxRow = 0;
        public InsertThiSinh()
        {
            InitializeComponent();         
            webservice.GetSessionValueCompleted += new EventHandler<GetSessionValueCompletedEventArgs>(webservice_GetSessionValueCompleted);
            webservice.GetSessionValueAsync();
            OKButton.IsEnabled = false;
            webservice.InsertThiSinhCompleted += new EventHandler<InsertThiSinhCompletedEventArgs>(webservice_InsertThiSinhCompleted);
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "";
            OKButton.Visibility = Visibility.Collapsed;
            bsyImport.IsBusy = true;
            OpenFileDialog Ofile = new OpenFileDialog();
            Ofile.Filter = "Excel (*.xls)|*.xls";
            if (Ofile.ShowDialog() == true)
            {
                FileStream fs = Ofile.File.OpenRead();
                Workbook book = Workbook.Open(fs);
                Worksheet sheet = book.Worksheets[0];
                sh = sheet;
                bool checkIdTruong = true;
                maxRow = sheet.Cells.LastRowIndex;
                if (sheet.Cells[sheet.Cells.FirstRowIndex, 0].StringValue != "MA SO THI SINH" || sheet.Cells[sheet.Cells.FirstRowIndex, 1].StringValue != "HO TEN" || sheet.Cells[sheet.Cells.FirstRowIndex, 2].StringValue != "MON 1" || sheet.Cells[sheet.Cells.FirstRowIndex, 3].StringValue != "MON 2" || sheet.Cells[sheet.Cells.FirstRowIndex, 4].StringValue != "MON 3" || sheet.Cells[sheet.Cells.FirstRowIndex, 5].StringValue != "DIEM UU TIEN" || sheet.Cells[sheet.Cells.FirstRowIndex, 6].StringValue != "MA NGANH DK" || sheet.Cells[sheet.Cells.FirstRowIndex, 7].StringValue != "DIA CHI" || sheet.Cells[sheet.Cells.FirstRowIndex, 8].StringValue != "CMT" || sheet.Cells[sheet.Cells.FirstRowIndex, 9].StringValue != "DIEN THOAI" || sheet.Cells[sheet.Cells.FirstRowIndex, 10].StringValue != "NGAY SINH" || sheet.Cells[sheet.Cells.FirstRowIndex, 11].StringValue != "MA TRUONG")
                {
                    MessageBox.Show("Yêu cầu nhập đúng thiết kế file Excel");
                }
                else
                {
                    for (int i = sheet.Cells.FirstRowIndex + 1; i <= sheet.Cells.LastRowIndex - 1; i++)
                    {
                        for (int k = sheet.Cells.FirstRowIndex + 2; k <= sheet.Cells.LastRowIndex; k++)
                        {
                            if (sheet.Cells[i, sheet.Cells.LastColIndex].StringValue != sheet.Cells[k, sheet.Cells.LastColIndex].StringValue)
                            {
                                checkIdTruong = false;
                            }
                        }
                    }
                    if (checkIdTruong == false)
                    {
                        MessageBox.Show("Yêu cầu kiểm tra cột Mã trường ");
                    }
                    else
                    {
                        webservice.CheckIdTruongCompleted += new EventHandler<CheckIdTruongCompletedEventArgs>(webservice_CheckIdTruongCompleted);
                        webservice.CheckIdTruongAsync(sheet.Cells[sheet.Cells.FirstRowIndex + 1, sheet.Cells.LastColIndex].StringValue);
                    }
                }     
            }
            this.bsyImport.IsBusy = false;
        }

        void webservice_CheckIdTruongCompleted(object sender, CheckIdTruongCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                MessageBox.Show("Chưa có dữ liệu trường.Yêu cầu nhập dữ liệu trường trước.");
                var cw = new InsertTruong();
                cw.Show();
            }
            else
            {
                INSERT(sh);               
            }
        }
        void webservice_InsertThiSinhCompleted(object sender, InsertThiSinhCompletedEventArgs e)
        {
            //bool Res = Convert.ToBoolean(e.Result);
            //if (Res)
            //{
            //    textBox1.Text += "Thêm mới thành công !!";
            //    this.textBox1.Text += Environment.NewLine;
            //}
            //else
            //{
            //    textBox1.Text += "Thêm mới thất bại !!";
            //    this.textBox1.Text += Environment.NewLine;
            //}

            textBox1.Text += e.Result;
            this.textBox1.Text += Environment.NewLine;

        }
        void webservice_GetSessionValueCompleted(object sender,GetSessionValueCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                manguoidung = Convert.ToInt32(e.Result);
                OKButton.IsEnabled = true;
            }
            else
            {
                MessageBox.Show("Yêu cầu đăng nhập lại để tiếp tục");
                App.NavigateToPage(new MainPage());
            }
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        void INSERT(Worksheet sheet)
        {
            string MASV = "";
            string HOTEN = "";
            double MON1 = 0;
            double MON2 = 0;
            double MON3 = 0;
            double DIEMUUTIEN = 0;
            int MANGANH = 0;
            string DIACHI = "";
            long CMT = 0;
            long DIENTHOAI = 0;
            string MATRUONG = "";
            DateTime NGAYSINH = DateTime.Now;
            for (int i = sheet.Cells.FirstRowIndex + 1; i <= sheet.Cells.LastRowIndex; i++)
            {
                for (int j = sheet.Cells.FirstColIndex; j <= sheet.Cells.LastColIndex; j++)
                {

                    switch (j)
                    {
                        case 0: MASV = sheet.Cells[i, j].StringValue;
                            break;
                        case 1: HOTEN = sheet.Cells[i, j].StringValue;
                            break;
                        case 2: MON1 = double.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 3: MON2 = double.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 4: MON3 = double.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 5: DIEMUUTIEN = double.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 6: MANGANH = Int32.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 7: DIACHI = sheet.Cells[i, j].StringValue;
                            break;
                        case 8: CMT = Int64.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 9: DIENTHOAI = Int64.Parse(sheet.Cells[i, j].StringValue);
                            break;
                        case 10: NGAYSINH =sheet.Cells[i, j].DateTimeValue;
                            break;
                        case 11: MATRUONG = sheet.Cells[i, j].StringValue;
                            break;
                    }
                }
                webservice.InsertThiSinhAsync(MATRUONG + MASV, HOTEN, MON1, MON2, MON3, DIEMUUTIEN, MANGANH, DIACHI, CMT, DIENTHOAI, NGAYSINH, manguoidung, MATRUONG);
            }
        }
        void CHECKID(Worksheet sheet)
        {
            webservice.CheckThiSinhCompleted += new EventHandler<CheckThiSinhCompletedEventArgs>(webservice_CheckThiSinhCompleted);
            for (int i = sheet.Cells.FirstRowIndex + 1; i <= sheet.Cells.LastRowIndex; i++)
            {
                webservice.CheckThiSinhAsync(sheet.Cells[i, 11].StringValue + sheet.Cells[i, 0].StringValue);
            }
        }

        void webservice_CheckThiSinhCompleted(object sender, CheckThiSinhCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                n++;
                check[n]= 0;
                
            }
            else
            {
                n++;
                check[n]=1; 
            }

            if (n == maxRow)
            {
                INSERT(sh);
            }

        }
    }
}

